package com.c.ctgproject.ui.collect

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryInWarehouseAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPowerBatteryInWerehouseBinding
import com.c.ctgproject.entity.guang.BatteryInWarehouseEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.google.gson.Gson
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult

/**
 *作者:LiBW
 *创建日期: 2021/9/16
 *描述:动力电池入库单查询 页面 散件过磅 页面
 */
class PowerBatteryInWarehouseActivity : BaseActivityV2(true) {
    private val mBinding by lazy { ActivityPowerBatteryInWerehouseBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var search = ""
    private var page = 1
    private val mList = ArrayList<BatteryInWarehouseEntity.DataBean.ListBean>()
    private var mAdapter: BatteryInWarehouseAdapter? = null
    private val mViewModel:PowerBatteryInWarehouseViewModel by lazy { ViewModelProvider(this)[PowerBatteryInWarehouseViewModel::class.java] }
    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1&&mViewModel.searchMine.value==false) {
            mList.removeAt(type.position)
            mAdapter?.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
        mViewModel.searchMine.observe(this, Observer {
            if (it){
                mBinding.mBottomRL.visibility= View.GONE
            }else{
                mBinding.mBottomRL.visibility= View.VISIBLE
            }
        })
        mViewModel.carIdList.observe(this, Observer {
            if(it.isEmpty()){
                mBinding.mConfirmBTN.text="过磅"
            }else{
                mBinding.mConfirmBTN.text="过磅（${it.size}）"
            }
        })
    }

    private var status: Int = 0
    private var searchMine = false

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
           // PowerBatteryInWarehouseSingleActivity
            if(mViewModel.carIdList.value?.size==0){
                showToast("请至少选择一辆车")
            }else{
                startActivityForResult<BatteryGetWeightActivity>(
                    1001,
                    MyConstants.ORDER_ID_LIST to Gson().toJson(mViewModel.carIdList.value))
            }
        }
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
//                "全部" -> {
//                    status=null
//                    page = 1
//                    getData()
//                }
                "待过磅" -> {
                    searchMine = false
                    mViewModel.changeSearchMine(false)
                    status = 0
                    page = 1
                    getData()
                }
//                "已过磅" -> {
//                    status=1
//                    page = 1
//                    getData()
//                }
                "我的" -> {
                    searchMine = true
                    mViewModel.changeSearchMine(true)
                    status = 1
                    page = 1
                    getData()
                }
            }
        }
        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
        mViewModel.clear()
        if (searchMine) {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.myBatteryPound(search, page), MyObserverHandlerLoading(this, loading) {
            //查询类型，不传等价于传1, 【1-我创建的 2-我过磅的 3-我入库的】
            ApiMethods.apiSubscribe(ApiMethods.retrofit.mineInWaitWidget(2, search, page), MyObserverHandlerLoading(this, loading,mBinding.mRefresh) {
                MyLog.d("我的过磅:$it")
                val data = getGson().fromJson(it, BatteryInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) {
                        mList.clear()
                        mAdapter = BatteryInWarehouseAdapter(this, mList, false,mViewModel=mViewModel)
                        mBinding.mRecyclerView.adapter = mAdapter
                        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
                    }

                    mList.addAll(list)
                    mAdapter?.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        } else {
//            ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryInWarehouse(search, page, status), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            //查询类型 2-待过磅 3-待入库
            ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryWaitIn(2, search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("电池待过磅回调:$it")
                val data = getGson().fromJson(it, BatteryInWarehouseEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) {
                        mList.clear()
                        mAdapter = BatteryInWarehouseAdapter(this, mList,mViewModel=mViewModel)
                        mBinding.mRecyclerView.adapter = mAdapter
                        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
                    }
                    mList.addAll(list)
                    mAdapter?.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)

                }
            })
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            1001->{
                if(resultCode==1){
                    getData()//提交成功后重新获取数据刷新界面
                }
            }
        }
    }
}